package com.hmdp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author <a href="https://gitee.com/hebei-dynasty-competition">搜一码赛</a>
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 查询店铺的所有类型
     *
     * @return
     */
    @Override
    public List<ShopType> queryShopTypeList() {
        String key="cache:shoptype";
        //1.判断一下redis中是否有店铺类型
        String shopTypeJson = stringRedisTemplate.opsForValue().get(key);
        System.out.println(shopTypeJson);
        if (StrUtil.isNotBlank(shopTypeJson)){
            //2.存在直接,返回所有店铺类型
            List<ShopType> shopTypeList =JSONUtil.toList(shopTypeJson, ShopType.class);
            System.out.println(shopTypeList);
            return shopTypeList;
        }
        //3.不存在,查询数据库
        List<ShopType> shopTypeList = query().orderByAsc("sort").list();
        //4.存到redis中
            stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shopTypeList));
        //5.返回数据
        return shopTypeList;
    }
}
